package models

import (
	"barrage-go/db"
	"log"
	"time"
)

func (BarrageMsg) TableName() string {
	return "t_barrage_msg"
}

type BarrageMsg struct {
	Id          int64     `json:"id"`
	UserId      int64     `json:"userId"`
	VideoId     int64     `json:"videoId"`
	MsgContent  string    `json:"msgContent"`
	MsgPosition int32     `json:"msgPosition"`
	MsgColor    string    `json:"msgColor"`
	VideoTime   int32     `json:"videoTime"`
	DelFlag     bool      `json:"delFlag"`
	CreateTime  time.Time `json:"createTime"`
	UpdateTime  time.Time `json:"updateTime"`
}

func GetHistoryBarrages(videoId string) []BarrageMsg {
	var barrages []BarrageMsg
	mysql := db.GetMysql()
	err := mysql.Debug().Where("del_flag = 0 and video_id = ?", videoId).Limit(80).Order("create_time desc").Find(&barrages).Error
	if err != nil {
		log.Panicln("查询历史弹幕数据发生异常:", err)
	}
	return barrages
}

func GetBarrageTotalCount(videoId string) int32 {
	var totalCount int64
	mysql := db.GetMysql()
	err := mysql.Debug().Where("del_flag = 0 and video_id=?", videoId).Find(&BarrageMsg{}).Count(&totalCount).Error
	if err != nil {
		log.Panicln("查询弹幕数据总数发生异常:", err)
	}
	return int32(totalCount)

}

func GetBarrageRollings(videoId string, videoTime int32) []BarrageMsg {
	var barrages []BarrageMsg
	mysql := db.GetMysql()

	err := mysql.Debug().Where("del_flag = 0 and video_id = ? and video_time = ?", videoId, videoTime).Find(&barrages).Error
	if err != nil {
		log.Panicln("查询滚动弹幕数据发生异常:", err)
	}
	return barrages
}

func SaveBarrageMsg(barrageMsg *BarrageMsg) {
	mysql := db.GetMysql()
	err := mysql.Debug().Save(barrageMsg).Error
	if err != nil {
		log.Panicln("保存弹幕数据发生异常:", err)
	}
}
